#include "Parser.h"

#include "net/instaweb/util/public/google_message_handler.h"

void CParser::Expose(void)
{
  py::class_<CParser, boost::noncopyable>("HtmlParser", py::no_init)
    .def(py::init<const std::string, const std::string>(
          (py::arg("url")=std::string(),
           py::arg("id")=std::string())))

    .add_property("url", &CParser::GetUrl)
    .add_property("id", &CParser::GetId)
    .add_property("linenum", &CParser::GetLineNumber)

    .def("reset", &CParser::Reset, "Reset this instance. Loses all unprocessed data.")
    .def("feed", &CParser::Feed, "Feed some data to the parser.")
    .def("close", &CParser::Close, "Handle the remaining data.")
    .def("flush", &CParser::Flush, "Flush the currently queued events through the filters.")
    ;
}

CParser::CParser(const std::string url, const std::string id)
  : m_message_handler(new GoogleMessageHandler())
{
  m_html_parser.reset(new HtmlParse(m_message_handler.get()));

  if (id.empty())
  {
    m_html_parser->StartParse(url);
  }
  else
  {
    m_html_parser->StartParseId(url, id);
  }
}

void CParser::Reset(void)
{
  std::auto_ptr<HtmlParse> html_parser(new HtmlParse(m_message_handler.get()));

  html_parser->StartParseId(m_html_parser->url(), m_html_parser->id());

  m_html_parser = html_parser;
}
